<?php
//这里是项目视图的模型

//浏览测评结果
//显示当前用户所在组的结果
function get_project_grade_this(){
	
	//session_start();
	$userid=$_SESSION['userid'];
	$c="userid='".$userid."'";
	$groupid=getdataallfdiyc('groupid','app_user',$c);
	foreach ($groupid as $key => $value) {
		get_project_grade($value);
	}
}
//显示所有用户的测评结果
function get_project_grade_all(){
	//获取所有组
	$allgroupid=getdataallf('groupid',"app_group");
	foreach ($allgroupid as $key => $value) {
		if($value){
			get_project_grade($value);
		}
		
	}
}

//浏览测评结果根据组序号查看
function get_project_grade($groupid){
	//初始化值
	$groupname='';//组名
	$sortid='';//所有项目ID
	$sort='';//所有项目
	$groupuserid='';//所有组员ID
	$groupuser='';//所有组员
	$usertable='';//所有组员的成绩
	

	//获取组名
	$groupname=getdataf('groupname','app_group','groupid',$groupid);

	//获取项目
	$sortid=getdataallfc('sortid','app_sort_group','groupid',$groupid);	
	foreach ($sortid as $tkey => $tvalue) {
		$sorttype=get_fatersort($tvalue);
		$sort[$sorttype][$tvalue]=getdataf('sortname','app_sort','sortid',$tvalue);
	}

	//获取组员
	$groupuserid=getdataallfc('userid','app_user','groupid',$groupid);
	//获取组员的信息
	$i=0;//标识
	foreach ($groupuserid as $tkey => $tvalue) {
		$usertable[$i]['userid']=$tvalue;
		$usertable[$i]['username']=getdataf('username','usertable','userid',$tvalue);
		
		//获取该用户所有项目的成绩
		foreach ($sort as $key => $value) {
			foreach ($value as $okey => $ovalue) {
				//获取当前项目的成绩
				$grade=0;
				$grade=(float)get_point($tvalue,$okey,$groupid);
				//获取参与评论的人数
				$num=1;
				$num=(float)getdatafcc("count(*)","app_user","appraisaled",1,"groupid",$groupid);
				//计算之后的成绩
				$cgrade=0;
				if($num){
					$cgrade=$grade/$num;
				}
				

				$usertable[$i]['usergrade'][$key][$okey]=sprintf("%.2f", $cgrade);  
			}
			//单项的计算总成绩
			$userdxgrade=0;
			$userdxgrade=get_finalpoint($usertable[$i]['usergrade'][$key]);
			$userdxgrade=sprintf("%.2f",$userdxgrade);
			$usertable[$i]['usergrade'][$key]['总分']=$userdxgrade;
		}
		$usertotlegrade=0;//用户总成绩
		$i++;
	}
	project_grade($groupname,$sort,$usertable);
}

//获取相对系数
function get_finalpoint($sort){
	$finalpoint=0;
	foreach ($sort as $key => $value) {
		$finalpoint=$finalpoint+sort_totlepointx($key)*$value;
	}
	return $finalpoint;
}
//获取项目相对总分的系数
function sort_totlepointx($sortid){
	$sortpoint=1;
	$fsort=get_sortfather($sortid);
	$sortpoint=(float)getdataf("sortpoint",'app_sort','sortid',$sortid);
	$sortupid=getdataf("sortupid",'app_sort','sortid',$sortid);
	$sortupmax=(float)getdataf("sortmax",'app_sort','sortid',$sortupid);
	$sortpoint=$sortpoint/$sortupmax;
	foreach ($fsort as $key => $value) {
		if($value>3){
			$thissortpoint=1;
			$sortupid=0;
			$sortupmax=1;
			$thissortpoint=(float)getdataf("sortpoint",'app_sort','sortid',$value);
			$sortupid=getdataf("sortupid",'app_sort','sortid',$value);
			$sortupmax=(float)getdataf("sortmax",'app_sort','sortid',$sortupid);
			$thissortpoint=$thissortpoint/$sortupmax;
			
			$sortpoint=$sortpoint*$thissortpoint;
		}
	}
	return $sortpoint;
}

//直接使用了sql查询语句
//获取分数
function get_point($userid,$sortid,$groupid){
	$row='';
	$query="select point from app_grade where userid='".$userid."' and sortid=".$sortid." and groupid='".$groupid."';";
	$result=mysql_query($query)or die(mysql_error());
	$row=mysql_fetch_array($result);
	return $row['point'];
}
?>